// Created by WXX on 2021/10/19 14:11
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * 执行用时：8 ms, 在所有 C++ 提交中击败了82.05%的用户
 * 内存消耗：9 MB, 在所有 C++ 提交中击败了94.14%的用户
 */
class Solution {
public:
    int projectionArea(vector<vector<int>>& grid) {
        int res = 0;
        int n = grid.size(), m = grid[0].size();
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                res += !!grid[i][j];
        // 加上每一列最大值
        for (int i = 0; i < m; i++) {
            int maxv = 0;
            for (int j = 0; j < n; j++) maxv = max(maxv, grid[j][i]);
            res += maxv;
        }
        // 加上每一行最大值
        for (int i = 0; i < n; i++) {
            int maxv = 0;
            for (int j = 0; j < m; j++) maxv = max(maxv, grid[i][j]);
            res += maxv;
        }
        return res;
    }
};

int main() {

    vector<vector<int>> grid = {
            {1, 2},
            {3, 4}
    };
    cout << Solution().projectionArea(grid) << endl;  // 17

    return 0;
}
